package com.artisan.spider.task;

import com.artisan.spider.consts.Consts;
import com.artisan.spider.domain.SpDept;
import com.artisan.spider.domain.SpDictionary;
import com.artisan.spider.service.ISpDeptService;
import com.artisan.spider.service.ISpDictionaryService;
import com.artisan.spider.service.ScheduledTaskJob;
import com.artisan.spider.util.PageUtil;
import com.artisan.spider.util.SpringContextUtil;
import com.microsoft.playwright.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用部分抓取（例如下拉或字典）
 */
@Slf4j
@Component
public class CommonTask implements ScheduledTaskJob{


    @Override
    public void run() {
        log.info("通用部分抓取,当前线程名称 {} ", Thread.currentThread().getName());
        try {
            collectionInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Transactional
    public void collectionInfo(){
        try (Playwright playwright = Playwright.create()) {
            String pageUrl = Consts.zysearch;
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate(pageUrl);

            //效力级别
            List<SpDictionary> dictList=new ArrayList();
            PageUtil.getSelectDic("zy_xljb",1,page, "#WebUCSearchFrom1_divXiaoLi > select > option",dictList,false);
            SpringContextUtil.getBean(ISpDictionaryService.class).saveBatch(dictList);
            log.info("------------中央效力级别---录入完成----------");

            //时效性 通用
            List<SpDictionary> xsList =new ArrayList();
            PageUtil.getSelectDic("zy_sxx",1,page, "#WebUCSearchFrom1_divShixiao > select > option",xsList,false);
            SpringContextUtil.getBean(ISpDictionaryService.class).saveBatch(xsList);
            log.info("------------中央和地方时效性---录入完成----------");

            //发布部门
            String fbbmUrl = Consts.zyfbbm;
            Page fbbmpage = context.newPage();
            fbbmpage.navigate(fbbmUrl);
            List<SpDept> zyfddeptList=new ArrayList();
            PageUtil.getSelectDept("zy_fbbm",1,page, "#listb_sor > option",zyfddeptList,true);
            SpringContextUtil.getBean(ISpDeptService.class).saveBatch(zyfddeptList);
            log.info("------------中央发布部门---录入完成----------");

            //批准部门
            String pzbmUrl = Consts.zypzbm;
            Page pzbmpage = context.newPage();
            pzbmpage.navigate(pzbmUrl);
            List<SpDept> zypzdeptList=new ArrayList();
            PageUtil.getSelectDept("zy_pzbm",1,pzbmpage, "#listb_sor > option",zypzdeptList,true);
            SpringContextUtil.getBean(ISpDeptService.class).saveBatch(zypzdeptList);
            log.info("------------中央批准部门---录入完成----------");


            //法规类别 地方和中央通用
            String fglbUrl = Consts.zyfglb;
            Page fglbPage = context.newPage();
            fglbPage.navigate(fglbUrl);
            List<SpDictionary> fglbList=new ArrayList();
            PageUtil.getSelectDic("zy_fglb",3,fglbPage, "#listb_sor > option",fglbList,true);
            SpringContextUtil.getBean(ISpDictionaryService.class).saveBatch(fglbList);
            log.info("------------中央和地方法规类别---录入完成----------");

            //地方法规-搜索页面
            String dffgUrl=Consts.dfsearch;;
            page.navigate(dffgUrl);


            //效力级别
            List<SpDictionary> dfxlList=new ArrayList();
            PageUtil.getSelectDic("df_xljb",4,page, "#WebUCSearchFrom1_divLarXiaoli > select > option",dfxlList,false);
            SpringContextUtil.getBean(ISpDictionaryService.class).saveBatch(dfxlList);
            log.info("------------地方效力级别---录入完成----------");


            // 发布部门
            String dffgfbbmUrl=Consts.dffbbm;
            Page dffbbmpage = context.newPage();
            dffbbmpage.navigate(dffgfbbmUrl);
            List<SpDept> dffbdeptList=new ArrayList();
            PageUtil.getSelectDept("df_fbbm",3,dffbbmpage,"#listb_sor > option",dffbdeptList,true);
            SpringContextUtil.getBean(ISpDeptService.class).saveBatch(dffbdeptList);
            log.info("------------地方发布部门---录入完成----------");



            // 批准部门
            String dffgpzbmmUrl=Consts.dfzbbm;
            Page dfpzbmpage = context.newPage();
            dffbbmpage.setDefaultTimeout(700000);
            dfpzbmpage.navigate(dffgpzbmmUrl);
            List<SpDept> dfpzdeptList=new ArrayList();
            PageUtil.getSelectDept("df_pzbm",1,dfpzbmpage, "#listb_sor > option",dfpzdeptList,true);
            SpringContextUtil.getBean(ISpDeptService.class).saveBatch(dfpzdeptList);
            log.info("------------地方批准部门---录入完成----------");

            page.close();
            context.close();
            browser.close();
        }
    }
}